package com.goods.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.ProductCategoryMapper;
import com.goods.business.mapper.ProductCategoryVOMapper;
import com.goods.business.mapper.ProductMapper;
import com.goods.business.service.ProductCategoryService;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.utils.CategoryTreeBuilder;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProductCategoryServiceImpl implements ProductCategoryService {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductCategoryMapper productCategoryMapper;
    @Autowired
    private ProductCategoryVOMapper productCategoryVOMapper;

    //查询显示所有分类列表及分类树结构
    @Override
    public PageVO<ProductCategoryTreeNodeVO> categoryTree(Integer pageNum, Integer pageSize) {


        List<ProductCategory> productCategories = productCategoryMapper.selectAll();

        //转VO
        List<ProductCategoryTreeNodeVO> treeNodeVOS = new ArrayList<>();

        if (!CollectionUtils.isEmpty(productCategories)) {
            for (ProductCategory productCategory : productCategories) {
                ProductCategoryTreeNodeVO treeNodeVO = new ProductCategoryTreeNodeVO();
                BeanUtils.copyProperties(productCategory, treeNodeVO);
                treeNodeVOS.add(treeNodeVO);
            }
        }
        PageHelper.startPage(pageNum,pageSize);

        List<ProductCategoryTreeNodeVO> build = CategoryTreeBuilder.build(treeNodeVOS);
        PageInfo<ProductCategoryTreeNodeVO> info = new PageInfo<>(build);
        return new PageVO<>(info.getTotal(), build);
    }

    //添加时 显示分类树结构
    @Override
    public List<ProductCategoryTreeNodeVO> getParentCategoryTree() {
        List<ProductCategory> productCategories = productCategoryMapper.selectAll();

        //转VO
        List<ProductCategoryTreeNodeVO> treeNodeVOS = new ArrayList<>();

        if (!CollectionUtils.isEmpty(productCategories)) {
            for (ProductCategory productCategory : productCategories) {
                ProductCategoryTreeNodeVO treeNodeVO = new ProductCategoryTreeNodeVO();
                BeanUtils.copyProperties(productCategory, treeNodeVO);
                treeNodeVOS.add(treeNodeVO);
            }
        }
        List<ProductCategoryTreeNodeVO> buildParent = CategoryTreeBuilder.buildParent(treeNodeVOS);
        return buildParent;
    }

    //添加分类
    @Override
    public void add(ProductCategory productCategory) {
//        ProductCategory productCategory = new ProductCategory();
//        BeanUtils.copyProperties(productCategoryVO,productCategory);
        productCategoryMapper.insert(productCategory);
    }

    //点击修改 回显分类数据
    @Override
    public ProductCategory edit(Long id) {
        ProductCategory productCategory = productCategoryMapper.selectByPrimaryKey(id);
        return productCategory;
    }

    //修改 保存
    @Override
    public void update(ProductCategory productCategory) {
        productCategoryMapper.updateByPrimaryKey(productCategory);
    }

    @Override
    public void delete(Long id) {
        //TODO 判断是否为父级分类，其下是否有子级分类
        productCategoryMapper.deleteByPrimaryKey(id);
    }



}
